//
//  MYGCalendarScheduleTableViewCell.swift
//  MYDoctor
//
//  Created by 王振明 on 2024/5/12.
//

import UIKit

class MYGCalendarScheduleTableViewCell: UITableViewCell {
    var selectBlock:(() -> ())?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        configUI()
    }
    @objc func selectAction(){
        if selectBlock != nil{
            self.selectBlock!()
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var model: MYGScheduleListModel = MYGScheduleListModel() {
        didSet {
            nameLabel.text = model.name
            timeLabel.text = "\(model.start_at) - \(model.end_at)"
            let typesArr = ["手术","线上会议","线下会议","培训","休假","待办","出差","自定义"]
            let colorArr = [RGBA(r: 0, g: 153, b: 255, a: 0.31),RGBA(r: 45, g: 199, b: 109, a: 0.15),RGBA(r: 255, g: 122, b: 82, a: 0.12),RGBA(r: 117, g: 64, b: 238, a: 0.09),RGBA(r: 255, g: 0, b: 0, a: 0.14),RGBA(r: 219, g: 151, b: 27, a: 0.13),RGBA(r: 152, g: 230, b: 37, a: 0.24),RGBA(r: 0, g: 255, b: 237, a: 0.12)]
            for ind in 0...typesArr.count - 1{
                if model.type == typesArr[ind]{
                    colorView.backgroundColor = colorArr[ind]
                    fromLabel.backgroundColor = colorArr[ind]
                    if model.is_color == 1{
                        whitebackView.backgroundColor = colorArr[ind]
                    }
                }
            }
            let dateString = model.set_date
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss" // 确保这与你的日期字符串格式相匹配
            let date = dateFormatter.date(from: dateString)
            fromLabel.setTitle(model.type, for: .normal)
            if model.schedule_status == 1 || model.schedule_status == 2 || model.schedule_status == 3{
                nameLabel.textColor = .rgba(169, 169, 169, 1)
                fromLabel.backgroundColor = .rgba(169, 169, 169, 1)
                timeLabel.textColor = .rgba(169, 169, 169, 1)
                colorView.backgroundColor = .rgba(169, 169, 169, 1)
                remindBtn.isSelected = true
                flashBtn.isSelected = true
            }
            if model.remind_time_type == 0{
                remindBtn.isHidden = true
            }
            if model.is_conflict == 1{
                flashBtn.isHidden = false
            }
        }
    }
    // MARK: - UI
    private func configUI() {
        contentView.backgroundColor = .clear
        self.backgroundColor = .clear
        contentView.addSubview(selectBtn)
        selectBtn.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(34)
            make.left.equalToSuperview().offset(16)
            make.size.equalTo(CGSize(width: 13, height: 13))
        }
        contentView.addSubview(whitebackView)
        whitebackView.snp.makeConstraints { make in
            make.left.equalTo(selectBtn.snp.right).offset(24)
            make.top.equalToSuperview()
            make.height.equalTo(80.5)
            make.right.equalToSuperview().offset(-19.5)
        }
        whitebackView.addSubview(colorView)
        colorView.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(4)
        }
        whitebackView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(colorView.snp.right).offset(8)
            make.right.equalToSuperview().offset(-72.5)
            make.top.equalToSuperview().offset(9.5)
            make.height.equalTo(18.5)
        }
        whitebackView.addSubviews([fromLabel,timeLabel,remindBtn,flashBtn])
        fromLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-11.5)
            make.top.equalToSuperview().offset(13)
            make.height.equalTo(12.5)
        }
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(colorView.snp.right).offset(8)
            make.top.equalTo(nameLabel.snp.bottom).offset(4)
            make.height.equalTo(18.5)
        }
        remindBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-5)
            make.centerY.equalTo(timeLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        flashBtn.snp.makeConstraints { make in
            make.right.equalTo(remindBtn.snp.left).offset(-5)
            make.centerY.equalTo(timeLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }

    }
    // MARK: - lazy load
    private lazy var selectBtn: UIButton = {
        let cont = UIButton()
//        cont.setImage(UIImage(named: "Chat/Menu/circle"), for: .normal)
        cont.layer.masksToBounds = true
        cont.layer.cornerRadius = 2.5
        cont.layer.borderWidth = 0.5
        cont.layer.borderColor = RGBA(r: 151, g: 151, b: 151, a: 1).cgColor
        cont.addTarget(self, action: #selector(selectAction), for: .touchUpInside)
        return cont
    }()
    private lazy var whitebackView:UIView = {
       let cont = UIView()
        cont.backgroundColor = .white
        cont.layer.cornerRadius = 4
        cont.clipsToBounds = true
        cont.layerShadowColor = .rgba(233, 231, 241, 1)
        cont.layerShadowOffset = CGSize(width: 0, height: 2.5)
        cont.layerShadowOpacity = 1
        cont.layerShadowRadius = 7.5
        return cont
    }()
    private lazy var colorView: UIView = {
        let vi = UIView()
        vi.backgroundColor = .rgba(23, 210, 100)
        return vi
    }()
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .rgba(51, 51, 51, 1)
        label.font = .medium(13)
        label.text = "关于个人工作台建设方案建设建…"
        return label
    }()
    private lazy var fromLabel: UIButton = {
        let label = UIButton()
        label.setTitleColor(.white, for: .normal)
        label.titleLabel!.font = .medium(9)
        label.contentEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        label.layerCornerRadius = 2.5
        return label
    }()
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.textColor = .rgba(69, 70, 77, 1)
        label.font = .regular(13)
        label.text = "3:40 PM - 5:40 PM"
        return label
    }()
    private lazy var remindBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "schedule_remind"), for: .normal)
        btn.setImage(UIImage(named: "schedule_remindGray"), for: .selected)
        return btn
    }()
    private lazy var flashBtn: UIButton = {
        let btn = UIButton()
        btn.isHidden = true
        btn.setImage(UIImage(named: "schedule_flash"), for: .normal)
        btn.setImage(UIImage(named: "schedule_flashGray"), for: .selected)
        return btn
    }()
}
